﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Windvoice.Blog;

namespace Windvoice.Areas.Manage.Controllers
{
    public class VoiceController : ManagerControllerBase
    {
        //
        // GET: /Manage/Voice/

        public ActionResult Index(int? page)
        {
            int PageIndex = page == null ? 1 : page.Value;
            int PageSize = 20;
            int RecordCount = 0;
            ViewBag.VoiceList = Voice.GetList(PageIndex, PageSize, String.Empty, out RecordCount);
            ViewBag.RecordCount = RecordCount;
            ViewBag.PageSize = PageSize;

            return View();
        }

        //
        // GET: /Manage/Voice/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Manage/Voice/Create

        [HttpPost]
        public ActionResult Create(Voice voice)
        {
            voice.CreateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                voice.Create();
                return RedirectToAction("index");
            }
            return View();
        }
        
        //
        // GET: /Manage/Voice/Edit/5
 
        public ActionResult Edit(int? id)
        {
            var voice = id == null ? null : Voice.Find(id.Value);
            if (voice != null)
            {
                return View(voice);
            }

            return View("_404");
        }

        //
        // POST: /Manage/Voice/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Voice voice)
        {
            var old = Voice.Find(voice.ID);
            voice.CreateTime = old.CreateTime;
            if (ModelState.IsValid)
            {
                voice.Update();
                return RedirectToAction("index");
            }

            return View(voice);
        }


        [HttpPost]
        public ActionResult Delete(FormCollection formValues)
        {
            if (!String.IsNullOrEmpty(formValues["id"]))
            {
                var VoiceID = Convert.ToInt32(formValues["id"]);
                Voice.Find(VoiceID).Delete();
                return Json(new { result = true, message = String.Empty });
            }
            return AjaxResult(result: false, message: "参数错误！");
        }
    }
}
